﻿using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataService.Office.Word
{
    /// <summary>
    /// 分页符
    /// </summary>
    public static class XmlPageBreak
    {
        /// <summary>
        /// 添加分节符
        /// </summary>
        public static void AddPageBreak(OpenXmlElement xmlElement)
        {
            bool isNewPpr = false;
            ParagraphProperties paragraphProperties220 = null;
            if (xmlElement is Paragraph)
            {
                Paragraph paragraph = xmlElement as Paragraph;
                paragraphProperties220 = paragraph.ParagraphProperties;
            }
            if (paragraphProperties220 == null)
            {
                paragraphProperties220 = new ParagraphProperties();
                isNewPpr = true;
            }
            SectionProperties sectionProperties1 = new SectionProperties();
            SectionType sectionType1 = new SectionType() { Val = SectionMarkValues.NextPage };

            sectionProperties1.Append(sectionType1);
            paragraphProperties220.Append(sectionProperties1);
            if (isNewPpr)
            {
                xmlElement.AppendChild(paragraphProperties220);
            }

        }




        /// <summary>
        /// 创建分节符；内容从新的一页开始
        /// </summary>
        /// <param name="xmlElement"></param>
        public static void AddNewPageBreak(OpenXmlElement xmlElement)
        {
            Paragraph pageParagraph = new Paragraph();

            ParagraphProperties paragraphProperties220 = new ParagraphProperties();

            SectionProperties sectionProperties1 = new SectionProperties();
            SectionType sectionType1 = new SectionType() { Val = SectionMarkValues.NextPage };

            sectionProperties1.Append(sectionType1);
            paragraphProperties220.Append(sectionProperties1);
            pageParagraph.Append(paragraphProperties220);
            xmlElement.Append(pageParagraph);
        }
        /// <summary>
        /// 创建纵向页分节符
        /// </summary>
        /// <returns></returns>
        public static Paragraph CreatePortraitSectionBreakParagraph()
        {
            Paragraph paragraph1 = new Paragraph() { RsidParagraphAddition = "00052B73", RsidRunAdditionDefault = "00052B73" };

            ParagraphProperties paragraphProperties1 = new ParagraphProperties();

            SectionProperties sectionProperties1 = new SectionProperties() { RsidR = "00052B73" };
            //  PageSize pageSize1 = new PageSize() { Width = (UInt32Value)16838U, Height = (UInt32Value)11906U, Orient = PageOrientationValues.Landscape };
            PageSize pageSize1 = new PageSize();
            //PageMargin pageMargin1 = new PageMargin() { Top = 1440, Right = (UInt32Value)1800U, Bottom = 1440, Left = (UInt32Value)1800U, Header = (UInt32Value)851U, Footer = (UInt32Value)992U, Gutter = (UInt32Value)0U };
            DocumentFormat.OpenXml.Wordprocessing.Columns columns1 = new DocumentFormat.OpenXml.Wordprocessing.Columns() { Space = "425" };
            DocGrid docGrid1 = new DocGrid() { Type = DocGridValues.Lines, LinePitch = 312 };

            sectionProperties1.Append(pageSize1);
            //  sectionProperties1.Append(pageMargin1);
            sectionProperties1.Append(columns1);
            sectionProperties1.Append(docGrid1);

            paragraphProperties1.Append(sectionProperties1);

            paragraph1.Append(paragraphProperties1);
            return paragraph1;
        }
        /// <summary>
        /// 在 元素前 行前插入分页符
        /// </summary>
        /// <param name="xmlElement"></param>
        public static void AddBreakBefore(OpenXmlElement xmlElement)
        {
            var breakType = new DocumentFormat.OpenXml.Wordprocessing.Break() { Type = BreakValues.Page };
            xmlElement.PrependChild<DocumentFormat.OpenXml.Wordprocessing.Break>(breakType);
        }

        /// <summary>
        /// 在 元素中插入分页符
        /// </summary>
        /// <param name="xmlElement"></param>
        public static Paragraph AddBreakAfter(OpenXmlElement xmlElement)
        {
            Paragraph paragraph1 = null;

            if (xmlElement is Paragraph)
            {
                paragraph1 = xmlElement as Paragraph;
            }
            else
            {
                paragraph1 = new Paragraph();
            }

            // 创建一个新的Run，并在其中添加分页符
            Run run = new Run();
            Break br = new Break();
            br.Type = BreakValues.Page; // 设置分页符类型为page
            run.Append(br);
            // 将分页符Run添加到段落
            paragraph1.Append(run);
            var breakParagraph = xmlElement.AppendChild(paragraph1);
            return breakParagraph;
        }

        public static bool LastElementIsSectionBreak(Body body)
        {
            // 获取最后一个段落
            var lastParagraph = body.Elements<Paragraph>().LastOrDefault();
            if (lastParagraph == null)
            {
                return false; // 没有段落，不可能是分节符
            }

            // 检查最后一个段落是否包含分节符
            var lastParagraphProperties = lastParagraph.Elements<ParagraphProperties>().LastOrDefault();
            if (lastParagraphProperties == null)
            {
                return false; // 最后一个段落没有段落属性
            }
            var run = lastParagraphProperties.Elements<Run>().LastOrDefault();
            if (run != null)
            {
                var runBreaks = run.Descendants<Break>();
                if (runBreaks != null)
                {
                    var breakElement = runBreaks.LastOrDefault();
                    if (breakElement != null && breakElement.Type == BreakValues.Page)
                    {
                        return true; // 发现分节符
                    }
                }
            }
            var sectionTypes = lastParagraphProperties.Descendants<SectionType>();
            if (sectionTypes != null)
            {
                bool istempFlag = false;
                foreach (SectionType item in sectionTypes)
                {
                    if (item != null && item.Val == SectionMarkValues.NextPage)
                    {
                        istempFlag = true; // 发现分节符
                        break;
                    }
                }
                return istempFlag;
            }


            return false; // 最后元素不是分节符
        }
    }
}
